package com.ruoyi.project.lesson.lessoncatalog.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.lesson.lesson.service.ILessonService;
import com.ruoyi.project.lesson.lessoncatalog.domain.LessonCatalog;
import com.ruoyi.project.lesson.lessoncatalog.service.ILessonCatalogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程目录Controller
 *
 * @author shixiangwen
 * @date 2021-05-10
 */
@Controller
@RequestMapping("/lesson/lessoncatalog")
public class LessonCatalogController extends BaseController {

    private String prefix = "lesson/lessoncatalog";

    @Autowired
    private ILessonCatalogService lessonCatalogService;

    @Autowired
    private ILessonService lessonService;

    @RequiresPermissions("lesson:lessoncatalog:view")
    @GetMapping("/{lessonId}")
    public String lessoncatalog(@PathVariable("lessonId") Long lessonId, ModelMap mmap) {
        mmap.put("lesson", lessonService.selectLessonById(lessonId));
        return prefix + "/lessoncatalog";
    }

    /**
     * 查询课程目录列表
     */
    @RequiresPermissions("lesson:lessoncatalog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LessonCatalog lessonCatalog) {
        startPage();
        List<LessonCatalog> list = lessonCatalogService.selectLessonCatalogList(lessonCatalog);
        return getDataTable(list);
    }

    /**
     * 导出课程目录列表
     */
    @RequiresPermissions("lesson:lessoncatalog:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LessonCatalog lessonCatalog) {
        List<LessonCatalog> list = lessonCatalogService.selectLessonCatalogList(lessonCatalog);
        ExcelUtil<LessonCatalog> util = new ExcelUtil<LessonCatalog>(LessonCatalog.class);
        return util.exportExcel(list, "lessoncatalog");
    }

    /**
     * 新增课程目录
     */
    @GetMapping("/add/{lessonId}/{type}")
    public String add(@PathVariable("lessonId") Long lessonId, @PathVariable("type") Integer type, ModelMap mmap) {
        mmap.put("lessonId", lessonId);
        mmap.put("type", type);
        mmap.put("lesson", lessonService.selectLessonById(lessonId));
        mmap.put("sort", lessonCatalogService.getMaxSort(lessonId));
        return prefix + "/add";
    }

    @GetMapping("/preview/{lessonId}")
    public String preview(@PathVariable("lessonId") Long lessonId, ModelMap mmap) {
        mmap.put("lessonId", lessonId);
        return prefix + "/preview";
    }


    /**
     * 新增保存课程目录
     */
    @RequiresPermissions("lesson:lessoncatalog:add")
    @Log(title = "课程目录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LessonCatalog lessonCatalog) {
        return toAjax(lessonCatalogService.insertLessonCatalog(lessonCatalog));
    }

    /**
     * 修改课程目录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        LessonCatalog lessonCatalog = lessonCatalogService.selectLessonCatalogById(id);
        mmap.put("lessonCatalog", lessonCatalog);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程目录
     */
    @RequiresPermissions("lesson:lessoncatalog:edit")
    @Log(title = "课程目录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LessonCatalog lessonCatalog) {
        return toAjax(lessonCatalogService.updateLessonCatalog(lessonCatalog));
    }

    /**
     * 删除课程目录
     */
    @RequiresPermissions("lesson:lessoncatalog:remove")
    @Log(title = "课程目录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(lessonCatalogService.deleteLessonCatalogByIds(ids));
    }
}
